﻿using CL.Enum.Common;
using CL.Enum.Common.Lottery;
using CL.Game.BLL;
using CL.Game.BLL.IM;
using CL.Game.Entity;
using CL.Tools.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.Admin.admin.room
{
    public partial class lotteryroomlist : UI.AdminPage
    {
        protected static int PageIndex = 0;
        protected static int PageSize = 10;
        protected static string RoomCode = string.Empty;
        protected static string RoomName = string.Empty;
        public static List<LotteriesEntity> LotteryInfos = new List<LotteriesEntity>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindLottery();
                BindData(string.Empty, string.Empty);
            }
        }
        protected void BindLottery()
        {
            LotteryInfos = new LotteriesBLL().QueryLotterys();
            ddlLottery.DataTextField = "LotteryName";
            ddlLottery.DataValueField = "LotteryCode";
            ddlLottery.DataSource = LotteryInfos;
            ddlLottery.DataBind();
        }
        protected void BindData(string Code, string Name, int Index = 0)
        {
            if (Index == 0)
                PageIndex = QPRequest.GetQueryInt("PageIndex", 1);
            else
                PageIndex = Index;
            if (!string.IsNullOrEmpty(Code))
                RoomCode = Code;
            else
                RoomCode = QPRequest.GetQueryString("RoomCode");
            if (!string.IsNullOrEmpty(Name))
                RoomName = Name;
            else
                RoomName = QPRequest.GetQueryString("RoomName");
            int LotteryCode = Convert.ToInt32(ddlLottery.SelectedValue);
            int RecordCount = 0;
            var Entitys = new LotteryRoomBLL().QueryListByLotteryCode(LotteryCode, RoomCode, RoomName, PageIndex, PageSize, ref RecordCount);
            rptList.DataSource = Entitys;
            rptList.DataBind();
            string pageUrl = string.Format("lotteryroomlist.aspx?PageIndex=__id__&RoomCode={0}&RoomName={1}", RoomCode, RoomName);
            PageContent.InnerHtml = Utils.OutPageList(PageSize, PageIndex, RecordCount, pageUrl, PageSize);
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName.Trim())
            {
                case "look":
                    Response.Redirect(string.Format("lookedit.aspx?id={0}", e.CommandArgument));
                    break;
                case "sync":
                    //同步IM服务器
                    long RoomID = Convert.ToInt64(e.CommandArgument);
                    var Entity = new LotteryRoomBLL().QueryEentity(RoomID);
                    if (Entity != null)
                    {
                        try
                        {
                            var IMRec = new Communication().SetRoom(RoomID.ToString(), Entity.RoomName, "-1", 1, Entity.LotteryCode, 1);
                            if (IMRec)
                                JscriptMsg(string.Format("同步成功"), "back");
                            else
                                JscriptMsg(string.Format("同步失败"), "back");
                        }
                        catch (Exception ex)
                        {
                            JscriptMsg(string.Format("同步失败，请联系管理员:{0}", ex.Message), "back");
                        }
                    }
                    else
                        JscriptMsg(string.Format("同步失败，请联系管理员"), "back");
                    break;
            }
        }

        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            this.BindData(string.Empty, string.Empty, 1);
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    PageIndex = _pagesize;
                    this.BindData(string.Empty, string.Empty, 0);
                    Utils.WriteCookie("lotteryroomlist_page_size", "QPcmsPage", _pagesize.ToString(), 14400);
                    string pageUrl = string.Format("lotteryroomlist.aspx?PageIndex={2}&RoomCode={0}&RoomName={1}", RoomCode, RoomName, _pagesize);
                    Response.Redirect(pageUrl);
                }
            }
        }
        protected string SetLottery(object obj)
        {
            string Rec = string.Empty;
            if (obj == null)
                Rec = "--";
            else
                Rec = Common.GetDescription((LotteryInfo)Convert.ToInt32(obj));
            return Rec;
        }
        protected string SetChat(object obj)
        {
            string Rec = string.Empty;
            if (obj == null)
                Rec = "--";
            else
            {
                switch (Convert.ToString(obj))
                {
                    case "all":
                        Rec = "无限制条件";
                        break;
                    case "full":
                        Rec = "实名认证";
                        break;
                    case "mobile":
                        Rec = "绑定手机号码";
                        break;
                    case "bank":
                        Rec = "绑定银行卡";
                        break;
                }
            }
            return Rec;
        }
    }
}